import requests
import json


class AddApprovalApi:
    def __init__(self):
        self.url = 'https://opencs.517la.com'
        self.applyReason = '城市数据单程单城市09'
        self.applyId = "TEST-20201029001"  # 申请单ID
        self.beginDate = "2020-12-20"  # 出发时间
        self.endDate = "2020-12-30"  # 到达时间
        self.fromCity = "北京"  # 出发地
        self.toCity = "上海"  # 目的地
        self.voyageType = '单程'  # 单程 or 往返
        self.canBookTime = '工作时间'  # 0 全部， 1 工作时间， 2 非工作时间
        self.bookPriceLimit = '不限制'  # 限制、不限制， 0（默认）不限制；1仅能预定最低价
        self.staffList = {  # 出行人列表
            {
                "empID": "9527",
                "staffName": "朱学恒"
            }
            , {
                "empID": "9529",
                "staffName": "朱北豆"
            }
            # ,            {
            #     "empID": "9530",
            #     "staffName": "朱南豆"
            # },            {
            #     "empID": "9531",
            #     "staffName": "朱一一"
            # },            {
            #     "empID": "9532",
            #     "staffName": "朱二二"
            # },            {
            #     "empID": "9533",
            #     "staffName": "朱三三"
            # },            {
            #     "empID": "9534",
            #     "staffName": "朱四四"
            # },            {
            #     "empID": "9535",
            #     "staffName": "朱五五"
            # }

        }

    def getToken(self):
        url = self.url + '/api/token/getToken'
        data = '{"data": {"appid": "201901221810524591741309", "appsecret": "secret98804"}, "requestid": "test",' \
               '"version": "0.0.1"} '
        headers = {'Content-Type': 'application/json;charset=UTF-8'}
        r = requests.post(url=url, data=data, headers=headers, verify=False)
        t = r.text
        jt = json.loads(t)
        print(jt)
        return jt['data']['access_token']

    def addApproval(self):
        c = {'单程': [0, "单程"], '往返': [1, "往返"]}
        ti = {'全部': [0, "全部"], '工作时间': [1, "工作时间"], '非工作时间': [2, "非工作时间"]}
        book = {'不限制': [0, "不限制"], '限制': [1, "仅能预定最低价"]}
        t = self.getToken()
        print(t)
        url = self.url + '/api/approval/addApproval'
        data = {
            "access_token": t,
            "data": [
                {
                    "applyId": self.applyId,
                    "applyList": [
                        {  # 改签
                            "staffList": self.staffList,
                            "voyageList": [
                                # {
                                #   "beginDate": self.beginDate,    # 出发时间
                                #   "endDate": self.endDate,  # 到达时间
                                #   "fromCityId": '120100',    # 出发城市ID
                                #   "toCityId ": '610100',      # 到达城市ID
                                #   "fromCity": self.fromCity,     # 出发地
                                #   "toCity": self.toCity,       # 目的地
                                #   "travelType": 0,
                                #   "travelTypeName": "飞机",
                                #   "voyageType": c[self.voyageType][0],
                                #   "travelNo": 1,
                                #   "voyageTypeName": c[self.voyageType][1]
                                # }
                                {
                                    "beginDate": "2021-01-09",
                                    "endDate": "2021-01-11",
                                    "toCity": "北京", "成都"
                                                    "wayCity": "force",
                                    "travelType": 0,
                                    "travelTypeName": "飞机",
                                    "voyageType": 0,
                                    "voyageTypeName": "单程"
                                }
                                #   ,
                                #   {
                                #   "beginDate": '2020-11-11',    # 出发时间
                                #   "endDate": '2020-11-13',  # 到达时间
                                #   "fromCity": '成都',     # 出发地
                                #   "toCity": '北京',       # 目的地
                                #   "travelType": 0,
                                #               "travelTypeName": "飞机",
                                #               "voyageType": '0',
                                #               "travelNo": 2,
                                #               "voyageTypeName": '单程'
                                # }

                            ],

                        }
                    ],
                    "applyPassState": 0,
                    "applyPurpose": "出差",
                    "applyReason": self.applyReason,
                    "costCenterName": "如山成本（所有人）",
                    "costCenterNo": "RSCB（SYR）",

                    "costCenterRatio": 0,
                    "costCenterPrice": 0,
                    "costCenterType": 1,
                    "canBookTime": ti[self.canBookTime][0],  # 时间限制 0（默认）全部；1工作时间；2非工作时间
                    "canBookTimeDesc": ti[self.canBookTime][1],
                    "bookPriceLimit": book[self.bookPriceLimit][0],  # 价格限制  0（默认）不限制；1仅能预定最低价
                    "bookPriceLimitDesc": book[self.bookPriceLimit][1],
                    "applyState": 1,  # 改签
                    # 委托人
                    # "consigneeStartTime": "2020-09-18 00:00:00",
                    # "consigneeEndTime": "2021-09-18 23:59:59",
                    # "consigneeStaffInfos": [
                    #     {
                    #         "consigneeEmpID": "9527",
                    #         "consigneeName": "9527"
                    #     }
                    # ],
                    "handoverStaff": {
                        "empID": "9527",
                        "staffName": "朱学恒"
                    },
                    # 'businessType': 1,      # 改签
                    "submitStaff": {
                        "empID": "9527",
                        "staffName": "朱学恒"
                    },
                }
            ],
            "requestid": "20180616112955089",
            "version": "0.0.1"
        }

        strdata = json.dumps(data)

        headers = {'Content-Type': 'application/json'}
        r = requests.post(url=url, data=strdata, headers=headers, verify=False)
        t = r.text
        print(t)


AddApprovalApi().addApproval()

# }, {
# "empID": "9528",
# "staffName": "元敬"
# }, {
# "empID": "l99",
# "staffName": "李九九"
# }, {
# "empID": "20201010",
# "staffName": "伯安"
# }, {
# "empID": "9529",
# "staffName": "明镜"
# }, {
# "empID": "yunhai001",
# "staffName": "云海"
# }, {
# "empID": "D123456",
# "staffName": "煜月"
# }, {
# "empID": "A00001",
# "staffName": "蓝浅"
# }, {
# "empID": "wangjia",
# "staffName": "雍佳"
# }
# , {
# "empID": "qs19951009",
# "staffName": "卿生"
# }
